#include <stdio.h>
#include <stdlib.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <unistd.h>

#include <sys/mman.h>
#include <sys/ioctl.h>


#define VERSION "fbplasma-" VERSION_STRING " (" __DATE__ ", " __TIME__ ")"


extern void plasma (unsigned char *frame_buffer, int screen_width, int screen_height);


int main (void) 
{
	printf("%s\n", VERSION);
	
	printf("opening /dev/fb0\n");
		
	int fd = open("/dev/fb0", O_RDWR);
	if (! fd)
	{
		printf("error: open /dev/fb0\n");
		return 1;
	}
	
	struct fb_var_screeninfo screeninfo;
	ioctl(fd, FBIOGET_VSCREENINFO, &screeninfo);

	printf(" * bits_per_pixel=%u\n", screeninfo.bits_per_pixel);
	printf(" * xres=%u\n", screeninfo.xres);
	printf(" * yres=%u\n", screeninfo.yres);
	
	if (screeninfo.bits_per_pixel != 32)
	{
		printf("invalid color depth, must be 32 bits per pixel\n");
		exit(1);
	}
	 
	printf("run plasma\n");
		
	unsigned char *frame_buffer = (unsigned char*) mmap(0, \
		screeninfo.xres * screeninfo.yres * 4, \
		PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);

	plasma(frame_buffer, screeninfo.xres, screeninfo.yres);
 
	return 0;
}


